<template>
  <div class="agentList divBox">
    <el-card class="box-card" shadow="hover">
      <template class="clearfix" v-slot:header>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="addPersonnel"
          >添加人员</el-button
        >
        <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-close"
              type="primary"
              @click="delRoles(null)"
              >批量删除人员</el-button
            >
          </div>
        </el-tooltip>
        <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-close"
              type="primary"
              @click="patchRoles(null, 1)"
              >批量停用人员</el-button
            >
          </div>
        </el-tooltip>
        <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-close"
              type="primary"
              @click="patchRoles(null, 0)"
              >批量启用人员</el-button
            >
          </div>
        </el-tooltip>
      </template>
      <el-table
        v-loading="disLoading"
        :data="tablePersonnelList"
        style="width: 100%"
        size="mini"
        class="table"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" min-width="55"></el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          align="center"
          min-width="60"
        ></el-table-column>
        <el-table-column
          prop="activityId"
          label="活动ID"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="realName"
          label="姓名"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="account"
          label="账号"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="password"
          label="密码"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="level"
          label="等级"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="systemRole.rules"
          label="身份"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column label="类型" align="center" min-width="100">
          <template slot-scope="{ row }">
            <span>{{ row.status == 1 ? "停用" : " 启用" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" fixed="right" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="edit(row.id)"
              >编辑</el-button
            >
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" size="small" @click="delRoles(row.id)"
              >删除</el-button
            >
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              size="small"
              @click="patchRoles(row.id, row.status == 1 ? 0 : 1)"
            >
              <span v-if="row.status == 0">停用人员</span>
              <span v-if="row.status == 1">启用人员</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="spreadBox">
        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.page"
          :limit.sync="pagination.limit"
          :page-sizes="[10, 20, 30, 50]"
          @pagination="replyChange"
        />
      </div>
    </el-card>
  </div>
</template>
    
    <script>
import {
  getRoleListApi,
  deleteRoleAllAp,
  patchRoleAllApi,
  getPersonnelListApi,
  patchPersonnelApi,
  deletePersonnelApi,
} from "@/api/system";
import { mapGetters } from "vuex";
let _this;

export default {
  name: "Index",
  data() {
    return {
      grid: {
        xl: 5,
        lg: 7,
        md: 12,
        sm: 12,
        xs: 24,
      },
      pagination: {
        page: 1,
        limit: 10,
        total: 0,
      },
      tablePersonnelList: [],
      disList: [],
      del: [],
      disLoading: false,
      tableLoading: false,
      disabled: true,
    };
  },
  computed: {
    ...mapGetters(["customRouter"]),
  },
  beforeCreate() {
    _this = this;
  },
  mounted() {
    this.getPersonnel();
  },
  methods: {
    /**
     * 行选中时
     */
    handleSelectionChange(val) {
      this.$set(
        this,
        "del",
        val.map((item) => item.id)
      );
      console.log(this.del);
      if (val.length > 0) this.$set(this, "disabled", false);
      else this.$set(this, "disabled", true);
    },
    /**
     * 添加角色
     */
    addPersonnel() {
      console.log(`${this.customRouter}/system/system_admin/personnelAdd`);
      this.$router.push({
        path: `${this.customRouter}/system/system_admin/personnelAdd`,
      });
    },
    /**
     * 编辑
     */
     edit(id) {
      this.$router.push({
        path: `${this.customRouter}/system/system_admin/personnelAdd`,
        query: { id: id },
      });
    },
    /**
     * 批量删除角色
     *
     */
    delRoles(id) {
      id || (this.del.length !== 0 && !this.disabled)
        ? this.$confirm(`删除人员`, {
            title: `删除人员`,
            message: this.$createElement(
              "div",
              null,
              ["确定要删除人员吗？", "删除人员后将无法恢复，请谨慎操作！"].map(
                (item) => this.$createElement("p", null, item)
              )
            ),
            showCancelButton: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              console.log(id, "id>>>>>>del:", `${this.del.join()}`);
              deletePersonnelApi(!id ? `${this.del.join()}` : id).then((res) => {
                this.getPersonnel();
                if (!id && res) return this.$message.warning(res);
                this.$message.success("删除人员成功！");
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: `已取消删除人员`,
              });
            })
        : this.$message.warning("请至少选择一条数据");
    },

    /**
     * 更新
     */
    patchRoles(id, statu) {
      console.log(id, "id, statu", statu);
      let data = [
        {
          op: "replace",
          path: "/status",
          value: statu,
        },
      ];
      let ids = !id ? `${this.del.join()}` : id;
      id || (this.del.length !== 0 && !this.disabled)
        ? this.$confirm(`修改人员状态`, {
            title: `修角人员状态`,
            message: this.$createElement(
              "div",
              null,
              ["确定要修改人员状态吗？"].map((item) =>
                this.$createElement("p", null, item)
              )
            ),
            showCancelButton: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              console.log(
                id,
                "id>>>>>>del:",
                `${this.del.join()}`,
                "data>>>>>>>:",
                data
              );
              patchPersonnelApi(ids, data).then((res) => {
                this.getPersonnel();
                if (!id && res) return this.$message.warning(res);
                this.$message.success("修改人员状态成功！");
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: `已取消修改人员状态`,
              });
            })
        : this.$message.warning("请至少选择一条数据");
    },
    /**
     * 获取角色信息
     */
    getPersonnel() {
      getPersonnelListApi({
        pageNumber: this.pagination.page,
        pageSize: this.pagination.limit,
        orderby: "Id desc",
        status: -1,
      }).then((res) => {
        this.$set(this, "tablePersonnelList", res.data);
        console.log("res>>>>>>>>>>>", res);
        this.pagination.total = res.head ? res.head.totalCount : 0;
      });
    },
    replyChange(reply) {
      this.$set(this.pagination, "page", reply.page);
      this.$set(this.pagination, "limit", reply.limit);
      console.log("pagination", this.pagination);
      this.getPersonnel();
    },
  },
};
</script>
    
<style lang="scss" scoped>
.agentList {
  .taoBox_img {
    width: 36px;
    height: 36px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
    font-size: 12px;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
}
</style>
    
<style lang="scss">
.el-dialog.cusDialogCls,
.el-dialog.editAgentDialogCls {
  max-width: 960px;
}
.el-dialog.promotionOrderDialogCls {
  max-width: 900px;
}
</style>
    